mini.parse();

function GoodsHandler() {
};

GoodsHandler.instance = new GoodsHandler();

GoodsHandler.isQueryBtnExpended = false;

//获得全局的商品数据表格对象
GoodsHandler.goodsGrid = mini.get("goodsGrid");

//加载表格数据
GoodsHandler.goodsGrid.load();

/**
 * 初始化商品查询面板
 */
GoodsHandler.initQueryForm = function(){
	GoodsQueryHandler.getBgbCombobox();
	GoodsQueryHandler.getBgsCombobox();
	mini.get("queryGoodsCommend").setData(GoodsQueryHandler.goodsCommendTypeData);
	mini.get("queryGoodsCommend").setValue(GoodsQueryHandler.goodsCommendTypeData[0].id);
	mini.get("goodsSortingCol").setData(GoodsQueryHandler.goodsSortingColData);
	mini.get("goodsSortingCol").setValue(GoodsQueryHandler.goodsSortingColData[0].id);
	mini.get("goodsSortingDir").setData(GoodsQueryHandler.goodsSortingDirData);
	mini.get("goodsSortingDir").setValue(GoodsQueryHandler.goodsSortingDirData[0].id);
}
GoodsHandler.initQueryForm();

/**
 * 根据用户权限展示功能按钮
 */
GoodsHandler.loadToolbar = function() {
	var array = new Array("goods_list_add", "goods_list_edit", "goods_list_picture","goods_list_discount","goods_list_promotion",
	"goods_list_accessories","goods_list_relation","goods_list_article","goods_list_query","goods_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsHandler.loadToolbar();

/**
 * 弹出添加商品窗口
 */
GoodsHandler.addGoods = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(774, 500, "添加商品");
	miniWin.setOption("bodyStyle","padding:0px;");
	miniWin.setOption("showMaxButton",true);
	
	miniWin.load("goodsListForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.GoodsFormHandler.passData(data);
	},function(action){
		GoodsHandler.goodsGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑商品窗口
 */
GoodsHandler.editGoods = function() {
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var rowsData = GoodsHandler.goodsGrid.getSelecteds();
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(800, 500, "编辑商品[<font color='blue'>"	+ rowData.bgiName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.setOption("showMaxButton",true);
		
		miniWin.load("goodsListForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bgiId: rowData.bgiId};
            iframe.contentWindow.GoodsFormHandler.passData(data);
		},function(action){
			GoodsHandler.goodsGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 商品前端预览处理程序
 */
GoodsHandler.viewGoodsById = function(){
	var msgId = mini.loading("功能实现中，敬请期待 ...", "系统提示");
 	setTimeout(function () {mini.hideMessageBox(msgId);}, 2000);
}

/**
 * 更新商品状态(放入回收站)
 */
GoodsHandler.updateGoodsStateById = function() {
	if (!MiniuiTools.hasPurview("goods_list_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	mini.confirm("确认把该商品放入回收站？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var rowData = GoodsHandler.goodsGrid.getSelected();
					var bgiId = rowData.bgiId;					
					GoodsHandler.goodsGrid.loading();
					
				    $.ajax({
				        url: __root + "/updateGoodsStateById.do",
				        data : {
							bgiId : bgiId,
							lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
						},
				        success: function (text) {
				            GoodsHandler.goodsGrid.reload();
				            GoodsHandler.goodsGrid.unmask();
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
				        	GoodsHandler.goodsGrid.unmask();
				            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
				        }
				    });	
				} 
			}
		)
};

/**
 * 更新精品推荐状态
 */
GoodsHandler.updateBgiBestById = function() {
	//权限控制代码
	if (!MiniuiTools.hasPurview("goods_list_edit")) {
		return;
	}
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var bgiId = rowData.bgiId;	
	GoodsHandler.goodsGrid.loading();
	
    $.ajax({
        url: __root + "/updateBgiBestById.do",
        data : {
			bgiId : bgiId,
			lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
		},
        success: function (text) {
            GoodsHandler.goodsGrid.reload();
            GoodsHandler.goodsGrid.unmask();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	GoodsHandler.goodsGrid.unmask();
            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        }
    });	
};

/**
 * 更新新品推荐状态
 */
GoodsHandler.updateBgiNewById = function() {
	//权限控制代码
	if (!MiniuiTools.hasPurview("goods_list_edit")) {
		return;
	}
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var bgiId = rowData.bgiId;	
	GoodsHandler.goodsGrid.loading();
	
    $.ajax({
        url: __root + "/updateBgiNewById.do",
        data : {
			bgiId : bgiId,
			lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
		},
        success: function (text) {
            GoodsHandler.goodsGrid.reload();
            GoodsHandler.goodsGrid.unmask();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	GoodsHandler.goodsGrid.unmask();
            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        }
    });	
};

/**
 * 更新热销商品推荐状态
 */
GoodsHandler.updateBgiHotById = function() {
	//权限控制代码
	if (!MiniuiTools.hasPurview("goods_list_edit")) {
		return;
	}
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var bgiId = rowData.bgiId;	
	GoodsHandler.goodsGrid.loading();
	
    $.ajax({
        url: __root + "/updateBgiHotById.do",
        data : {
			bgiId : bgiId,
			lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
		},
        success: function (text) {
            GoodsHandler.goodsGrid.reload();
            GoodsHandler.goodsGrid.unmask();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	GoodsHandler.goodsGrid.unmask();
            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        }
    });	
};

/**
 * 更新团购商品推荐状态
 */
GoodsHandler.updateBgiBuyById = function() {
	//权限控制代码
	if (!MiniuiTools.hasPurview("goods_list_edit")) {
		return;
	}
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var bgiId = rowData.bgiId;	
	GoodsHandler.goodsGrid.loading();
	
    $.ajax({
        url: __root + "/updateBgiBuyById.do",
        data : {
			bgiId : bgiId,
			lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
		},
        success: function (text) {
            GoodsHandler.goodsGrid.reload();            
            GoodsHandler.goodsGrid.unmask();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	GoodsHandler.goodsGrid.unmask();
            MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        }
    });	
};

/**
 * 展开或折叠查询商品面板
 */
GoodsHandler.queryGoods = function() {
	if (GoodsHandler.isQueryBtnExpended) {
		GoodsHandler.isQueryBtnExpended = false;
		mini.get("goods_list_query").setText("展开查询");
		mini.get("goods_list_query").setIconCls("icon-btn-expand");
	} else {
		GoodsHandler.isQueryBtnExpended = true;
		mini.get("goods_list_query").setText("折叠查询");
		mini.get("goods_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsDiv").toggle(500);
    setTimeout(function(){mini.get("goodsPanel").doLayout();},600);
};

/**
 * 执行查询商品的方法
 */
GoodsHandler.doQueryGoods = function(){
	var goodsQueryData = GoodsQueryHandler.goodsQueryForm.getData();
	var json = mini.encode(goodsQueryData);
    GoodsHandler.goodsGrid.load({goodsQueryData: json});
};

/**
 * 刷新页面数据及布局
 */
GoodsHandler.afresh = function(){
	GoodsHandler.doQueryGoods();
	mini.get("goodsPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GoodsHandler.goodsGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

      if (column.field == "bgiBest") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsHandler.updateBgiBestById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsHandler.updateBgiBestById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
     if (column.field == "bgiNew") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsHandler.updateBgiNewById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsHandler.updateBgiNewById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
     if (column.field == "bgiHot") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsHandler.updateBgiHotById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsHandler.updateBgiHotById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
     if (column.field == "bgiBuy") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsHandler.updateBgiBuyById();'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsHandler.updateBgiBuyById();'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if(column.field == "bgiOperation"){
    	e.cellHtml = "<span class='icon-show-view' style='cursor: pointer;' onclick='GoodsHandler.viewGoodsById();'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" 
    			   + "<span class='icon-show-trash' style='cursor: pointer;' onclick='GoodsHandler.updateGoodsStateById();'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
    }
});

//跳转至商品配件并传递参数
GoodsHandler.accessories = function() {
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var rowsData = GoodsHandler.goodsGrid.getSelecteds();
	if(rowsData.length == 1){
		var href = encodeURI(encodeURI("goodsAccessories.jsp?bgiId="+rowData.bgiId+"&bgiName="+rowData.bgiName));
		window.location.href = href;
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
}

//跳转至商品关联并传递参数
GoodsHandler.association = function() {
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var rowsData = GoodsHandler.goodsGrid.getSelecteds();
	if(rowsData.length == 1){
		var href = encodeURI(encodeURI("goodsAssociation.jsp?bgiId="+rowData.bgiId+"&bgiName="+rowData.bgiName));
		window.location.href = href;
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
}

//跳转至文章关联并传递参数
GoodsHandler.article = function() {
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var rowsData = GoodsHandler.goodsGrid.getSelecteds();
	if(rowsData.length == 1){
		var href = encodeURI(encodeURI("goodsArticles.jsp?bgiId="+rowData.bgiId+"&bgiName="+rowData.bgiName));
		window.location.href = href;
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
}

//跳转至商品相册并传递参数
GoodsHandler.addGoodsPicture = function() {
	var rowData = GoodsHandler.goodsGrid.getSelected();
	var rowsData = GoodsHandler.goodsGrid.getSelecteds();
	if(rowsData.length == 1){
		window.location.href="goodsPicture.jsp?bgiId="+rowData.bgiId+"&bgiName="+rowData.bgiName;
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
}

/**
 * 商品优惠处理程序
 */
GoodsHandler.goodsDiscount = function(){
	var msgId = mini.loading("功能实现中，敬请期待 ...", "系统提示");
 	setTimeout(function () {mini.hideMessageBox(msgId);}, 2000);
}

/**
 * 商品促销处理程序
 */
GoodsHandler.goodsPromotion = function(){
	var msgId = mini.loading("功能实现中，敬请期待 ...", "系统提示");
 	setTimeout(function () {mini.hideMessageBox(msgId);}, 2000);
}
